package com.ruoyi.vplat.websocket.handler;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.entity.SysAttachment;
import com.ruoyi.common.websocket.handler.AbstractWebsocketMsgHandler;
import com.ruoyi.common.websocket.server.WebSocketUtils;
import com.ruoyi.system.service.ISysAttachmentService;
import com.ruoyi.vplat.service.IVplatGatewayService;
import com.ruoyi.vplat.service.IVplatVideoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

@Component
@Slf4j
public class WebsocketMsgHandler18 extends AbstractWebsocketMsgHandler {
    @Autowired
    private ApplicationEventPublisher eventPublisher;
    @Autowired
    private IVplatGatewayService gatewayService;
    @Autowired
    private IVplatVideoService vplatVideoService;
    @Autowired
    private ISysAttachmentService attachmentService;
    public static final Map<String, Object> commands = new ConcurrentHashMap<>();

    @Override
    public void invoke(JSONObject message) {//网关第二次响应
        log.info("录像服务第二次响应:{}", message);
        JSONObject param = message.getJSONObject("param");
        JSONObject command = message.getJSONObject("command");
        String szClientId = command.getString("szClientId");
        Integer iSeqNum = command.getInteger("iSeqNum");
        Integer iProtocol = command.getInteger("iProtocol");
        String szDevId = param.getString("szDevId");
        Integer iRecId = param.getInteger("iRecId");
        String key = szClientId + "_" + iSeqNum;

        String szRecUrl = (String) param.get("szRecUrl");
        String szImgUrl = (String) param.get("szImgUrl");
        if(StringUtils.isNotBlank(szRecUrl) || StringUtils.isNotBlank(szImgUrl)){
            saveAttachment(param, szDevId, szImgUrl, szRecUrl);

        }
        synchronized (key.intern()) {
            CountDownLatch countDownLatch = (CountDownLatch) commands.get(key);
            if (countDownLatch != null) {//唤醒阻塞等待结果的线程
                commands.put(key, message);
                countDownLatch.countDown();
            } else {//返回录像结果至请求者
                WebSocketUtils.sendMessage(szClientId, message);
            }
        }
    }

    private void saveAttachment(JSONObject param, String szDevId, String szImgUrl, String szRecUrl) {
        try {
            String szStartTime = (String) param.get("szStartTime");
            String szEndTime = (String) param.get("szEndTime");
            String iRecordDuration = (String) param.get("iRecordDuration");
            String iRecSize = (String) param.get("iRecSize");
            String iImgSize = (String) param.get("iImgSize");
            String szType = (String) param.get("szType");
            Date createTime = DateUtils.parseDate(szStartTime, new String[]{"yyyy-MM-dd HH:mm:ss"});
            SysAttachment attachment = new SysAttachment();
            attachment.setName(szDevId + "_" + DateFormatUtils.format(createTime, "yyyyMMddHHmmss"));
            //attachment.setCatagory(szType);

            attachment.setCreateTime(createTime);
            if (StringUtils.isNotBlank(szImgUrl)) {
                attachment.setStoragePath(szImgUrl);
                attachment.setCatagory("0");
                attachmentService.insert(attachment);
            }
            if (StringUtils.isNotBlank(szRecUrl)) {
                attachment.setId(UUID.randomUUID().toString());
                attachment.setStoragePath(szRecUrl);
                attachment.setCatagory("1");
                attachmentService.insert(attachment);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("保存附件异常", e);
        }
    }
}
